﻿#ifndef __SS_MESSAGE_POOL_H__
#define __SS_MESSAGE_POOL_H__
//#include "../container/ssObjectPool.h"
#include "../memory/ssMutexObjectPool.h"


namespace StarSeeker
{

struct ssMsgBufBase_s
{
protected:
	u32 maxLen;
	char* pDate;
public:
	u32 len;

	u32 MaxLen() {return maxLen;}
	char* Data() {return pDate;}
};
struct ssMsgBuf512_s : public ssMsgBufBase_s
{
private:
	char data[512];
public:
	ssMsgBuf512_s()
	{
		memset(data, 0, 512);
		len = 0;
		maxLen = 512;
		pDate = data;
	}
};
struct ssMsgBuf1_s : public ssMsgBufBase_s
{
private:
	char data[1024];
public:
	ssMsgBuf1_s()
	{
		memset(data, 0, 1024);
		len = 0;
		maxLen = 1024;
		pDate = data;
	}
};
struct ssMsgBuf10_s : public ssMsgBufBase_s
{
private:
	char data[1024*10];
public:
	ssMsgBuf10_s()
	{
		memset(data, 0, sizeof(data));
		len = 0;
		maxLen = 1024*10;
		pDate = data;
	}
};
struct ssMsgBuf40_s : public ssMsgBufBase_s
{
private:
	char data[1024*40];
public:
	ssMsgBuf40_s()
	{
		memset(data, 0, sizeof(data));
		len = 0;
		maxLen = 1024*40;
		pDate = data;
	}
};
struct ssMsgBuf80_s : public ssMsgBufBase_s
{
private:
	char data[1024*80];
public:
	ssMsgBuf80_s()
	{
		memset(data, 0, sizeof(data));
		len = 0;
		maxLen = 1024*80;
		pDate = data;
	}
};

#define SS_MSG_POOL(T1)	class ssMsgPool_##T1\
{\
public:\
	ssMsgBufBase_s* Malloc(u32 size)\
	{\
		if (size<512)\
		{\
			return m_msg512_pool.Malloc();\
		}\
		else if (size<1024)\
		{\
			return m_msg1_pool.Malloc();\
		}\
		else if (size<1024*10)\
		{\
			return m_msg10_pool.Malloc();\
		}\
		else if (size<1024*40)\
		{\
			return m_msg40_pool.Malloc();\
		}\
		else if (size<1024*80)\
		{\
			return m_msg80_pool.Malloc();\
		}\
		return 0;\
	}\
	void Free(ssMsgBufBase_s* ps)\
	{\
		if (ps->MaxLen()==512)\
		{\
			m_msg512_pool.Free(SS_STATIC_CAST(ssMsgBuf512_s*,ps));\
		}\
		else if (ps->MaxLen()==1024)\
		{\
			m_msg1_pool.Free(SS_STATIC_CAST(ssMsgBuf1_s*,ps));\
		}\
		else if (ps->MaxLen()==1024*10)\
		{\
			m_msg10_pool.Free(SS_STATIC_CAST(ssMsgBuf10_s*,ps));\
		}\
		else if (ps->MaxLen()==1024*40)\
		{\
			m_msg40_pool.Free(SS_STATIC_CAST(ssMsgBuf40_s*,ps));\
		}\
		else if (ps->MaxLen()==1024*80)\
		{\
			m_msg80_pool.Free(SS_STATIC_CAST(ssMsgBuf80_s*,ps));\
		}\
	}\
private:\
	T1<ssMsgBuf512_s>		m_msg512_pool;\
	T1<ssMsgBuf1_s>			m_msg1_pool;\
	T1<ssMsgBuf10_s>		m_msg10_pool;\
	T1<ssMsgBuf40_s>		m_msg40_pool;\
	T1<ssMsgBuf80_s>		m_msg80_pool;\
}

typedef SS_MSG_POOL(ssObjPool)		ssMsgPool_t;
typedef SS_MSG_POOL(ssMutexObjPool)	ssMutexMsgPool_t;

}
#endif
